<?php

require_once dirname(__FILE__).'/../lib/auditoriaGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/auditoriaGeneratorHelper.class.php';

/**
 * auditoria actions.
 *
 * @package    plagas
 * @subpackage auditoria
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class auditoriaActions extends autoAuditoriaActions
{
  public function executeIndex(sfWebRequest $request)
  {
    // sorting
    if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort')))
    {
      $this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
    }

    // pager
    if ($request->getParameter('page'))
    {
      $this->setPage($request->getParameter('page'));
    }

    $this->pager = $this->getPager();
    $this->sort = $this->getSort();
    $this->historicos = null;
    $this->archivarForm = new archivarFormFilter();
  }

  public function executeFilter(sfWebRequest $request)
  {
    $this->archivarForm = new archivarFormFilter();
    $this->historicos = null;
    $this->setPage(1);

    if ($request->hasParameter('_reset'))
    {
      $this->setFilters($this->configuration->getFilterDefaults());

      $this->redirect('@auditoria');
    }

    $this->filters = $this->configuration->getFilterForm($this->getFilters());
    $allFormValues = $request->getParameter($this->filters->getName());
    $this->filters->bind($request->getParameter($this->filters->getName()));
    if ($this->filters->isValid())
    {
      $this->setFilters($this->filters->getValues());

      if ($allFormValues["historico"])
      {
        $valoresFiltro = $request->getParameter($this->filters->getName());
        $tabla = $valoresFiltro["tabla"];
        $accion = $valoresFiltro["accion"]["text"];
        $usuario = $valoresFiltro["usuario_id"];

        $this->historicos = Doctrine_Core::getTable('historico')->getHistoricos($tabla,$accion,$usuario);
      }
      $this->setTemplate('index');
    }

    $this->pager = $this->getPager();
    $this->sort = $this->getSort();

    $this->setTemplate('index');
  }

  public function executeArchivar(sfWebRequest $request)
  {
    $this->archivarForm = new archivarFormFilter();
    $formFilter = $request->getParameter($this->archivarForm->getName());
    $hasta_fecha = $formFilter["hasta_fecha"];
    $hasta_fecha = $hasta_fecha["year"].'-'.$hasta_fecha["month"].'-'.$hasta_fecha["day"];
    Doctrine_Core::getTable('auditoria')->delete($hasta_fecha);
    $this->redirect('auditoria');
  }
}
